package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.GeologicHazardQuery;
import com.deyuanyun.pic.ae.domain.GeologicHazard;
import com.deyuanyun.pic.ae.domain.GeologicHazardExample;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.domain.SewerExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.enumeration.PositionRelationEnum;
import com.deyuanyun.pic.ae.mapper.GeologicHazardMapper;
import com.deyuanyun.pic.ae.mapper.dao.GeologicHazardDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.GeologicHazardService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.util.PipeMgrUtil;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “管道沿线地质灾害信息” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class GeologicHazardServiceImpl extends AEBaseService implements GeologicHazardService {

    @Resource
    private GeologicHazardDao geologicHazardDao;
    @Resource
    private GeologicHazardMapper geologicHazardMapper;

    @Override
    public Page<GeologicHazard> query(GeologicHazardQuery geologicHazardQuery) {
        return geologicHazardDao.query(geologicHazardQuery);
    }

    @Override
    public GeologicHazard find(String id) {
        return geologicHazardMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(GeologicHazard geologicHazard, List<String> imgs) {

        Identification beginIdentification = PipeMgrUtil.queryIdentificationFlag(geologicHazard.getBeginNagmInput());
        Asserts.assertLogic((beginIdentification==null), "起点附近测试桩:“"+geologicHazard.getBeginNagmInput()+"”不存在，请重新输入");
        geologicHazard.setBeginNagmInput(beginIdentification.getId());
        geologicHazard.setBeginNagmNumberInput(beginIdentification.getSignNumber());
        Identification endIdentification = PipeMgrUtil.queryIdentificationFlag(geologicHazard.getEndNagmInput());
        Asserts.assertLogic((endIdentification==null), "终点附近测试桩:“"+geologicHazard.getEndNagmInput()+"”不存在，请重新输入");
        geologicHazard.setEndNagmInput(endIdentification.getId());
        geologicHazard.setEndNagmNumberInput(endIdentification.getSignNumber());

        // 计算起始绝对里程
        PositionObj beginPositionObj = getPositionInfo(geologicHazard.getPipeLineId(), geologicHazard.getBeginX(), geologicHazard.getBeginY());
        if (beginPositionObj != null) {
            geologicHazard.setBeginMileage(beginPositionObj.getMileage());
            geologicHazard.setBeginNagm(beginPositionObj.getNagm());
            geologicHazard.setBeginNagmNumber(beginPositionObj.getNagmNumber());
            geologicHazard.setBeginNagmMileage(beginPositionObj.getNagmMileage());
        }
        // 计算终点绝对里程
        PositionObj endPositionObj = getPositionInfo(geologicHazard.getPipeLineId(), geologicHazard.getEndX(), geologicHazard.getEndY());
        if (endPositionObj != null) {
            geologicHazard.setEndMileage(endPositionObj.getMileage());
            geologicHazard.setEndNagm(endPositionObj.getNagm());
            geologicHazard.setEndNagmNumber(endPositionObj.getNagmNumber());
            geologicHazard.setEndNagmMileage(endPositionObj.getNagmMileage());
        }


        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(geologicHazard.getId()) ? geologicHazard.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(geologicHazard.getId())){
//            GeologicHazardExample example = new GeologicHazardExample();
//            example.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andCustomNumberEqualTo(geologicHazard.getCustomNumber());
//            int countNum = geologicHazardMapper.countByExample(example);
//            Asserts.assertLogic(countNum > 0, "编号为:“"+geologicHazard.getCustomNumber()+"”管道沿线地质灾害 已经存在，不能重复插入");

            geologicHazard.setId(bizzId);
            geologicHazard.setCreateDate(new Date());
            geologicHazard.setCreateUser(realName);
            geologicHazard.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            geologicHazard.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return geologicHazardMapper.insertSelective(geologicHazard);
        }

        GeologicHazard geologicHazardObj = this.find(geologicHazard.getId());
//        Asserts.assertLogic(geologicHazardObj == null, "编号为:“"+geologicHazard.getCustomNumber()+"”管道沿线地质灾害 不存在，不允许修改");
//        Asserts.assertLogic(isAuditReleased(geologicHazardObj.getDataStated()), "编号为:“"+geologicHazard.getCustomNumber()+"”管道沿线地质灾害 已审核通过,不允许编辑");
        Asserts.assertLogic(geologicHazardObj == null, "管道沿线地质灾害 不存在，不允许修改");
        Asserts.assertLogic(isAuditReleased(geologicHazardObj.getDataStated()), "管道沿线地质灾害 已审核通过,不允许编辑");
        if (isAuditDismissed(geologicHazardObj.getDataStated())){
            geologicHazard.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return geologicHazardMapper.updateByPrimaryKeySelective(geologicHazard);
    }

    @Override
    public int audit(GeologicHazard geologicHazard) {
        GeologicHazard geologicHazardObj = this.find(geologicHazard.getId());
//        Asserts.assertLogic(geologicHazardObj == null, "编号为:“"+geologicHazardObj.getCustomNumber()+"”管道沿线地质灾害 不存在，不允许修改");
//        Asserts.assertLogic(isAudit(geologicHazardObj.getDataStated()), "编号为:“"+geologicHazardObj.getCustomNumber()+"”管道沿线地质灾害 已审核,不允许重复审核");
        Asserts.assertLogic(geologicHazardObj == null, "管道沿线地质灾害 不存在，不允许修改");
        Asserts.assertLogic(isAudit(geologicHazardObj.getDataStated()), "管道沿线地质灾害 已审核,不允许重复审核");
        return geologicHazardMapper.updateByPrimaryKeySelective(geologicHazard);
    }

    @Override
    public int delete(String id) {
        GeologicHazard geologicHazard = this.find(id);
//        Asserts.assertLogic(geologicHazard == null, "编号为:“"+geologicHazard.getCustomNumber()+"”管道沿线地质灾害 不存在，不允许删除");
//        Asserts.assertLogic(isAuditReleased(geologicHazard.getDataStated()), "编号为:“"+geologicHazard.getCustomNumber()+"”管道沿线地质灾害 已审核通过,不允许删除");
        Asserts.assertLogic(geologicHazard == null, "管道沿线地质灾害 不存在，不允许删除");
        Asserts.assertLogic(isAuditReleased(geologicHazard.getDataStated()), "管道沿线地质灾害 已审核通过,不允许删除");
        geologicHazard = new GeologicHazard();
        geologicHazard.setId(id);
        geologicHazard.setDeleted(Short.valueOf("1"));
        geologicHazard.setUpdateDate(new Date());
        geologicHazard.setUpdateUser(realName);
        geologicHazard.setUpdateRemark("删除");
        return geologicHazardMapper.updateByPrimaryKeySelective(geologicHazard);
    }


}
